package com.qf.controller;

import com.alibaba.fastjson.JSON;
import com.qf.bean.AddressBean;
import com.qf.pojo.User;
import com.qf.service.AddressesService;
import com.qf.service.Impl.AddressesServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;

@WebServlet("/addressesFindAllController.action")
public class AddressesFindAllController extends HttpServlet {

    private AddressesService addressesService = new AddressesServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //从session中获取用户对象
        HttpSession session = req.getSession(false);
        User user = (User)session.getAttribute("user");
        //查询商品
        List<AddressBean> addressBeanList =  addressesService.findAll(user.getUid());
        //创建Map集合
        HashMap<String, Object> map = new HashMap<>();
        //判断
        if(addressBeanList !=null && addressBeanList.size() > 0){
            //返回给前端
            map.put("data",addressBeanList);
        }
        //返回数据给前端
        resp.getWriter().write(JSON.toJSONString(map));

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }
}
